﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Background.Core.Entity;
using Commons.Entity;
using System.Linq.Expressions;
namespace Background.Core.DAL
{
    public class WebManageMenusRepository : BackgroundRepositoryBase
    {
        static WebManageMenusRepository()
        {
            WebManageMenusCache = new WebManageMenusRepository().GetEntities<WebManageMenus>().ToList();
        }
        static List<WebManageMenus> WebManageMenusCache;
        /// <summary>
        /// 根据菜单级别得到列表
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public List<WebManageMenus> GetWebManageMenusByLevel(int level)
        {
            return base.GetEntities<WebManageMenus>(i => i.MenuLevel == level).ToList();
        }
        /// <summary>
        /// 根据父ＩＤ得到菜单列表
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public List<WebManageMenus> GetWebManageMenusByParentID(int parentID)
        {
            return base.GetEntities<WebManageMenus>(i => i.ParentID == parentID).ToList();
        }

        /// <summary>
        /// 得到完整的菜单树
        /// </summary>
        /// <returns></returns>
        public WebManageMenus WebManageMenusTree()
        {
            WebManageMenus WebManageMenu = new WebManageMenus() { MenuID = 0 };
            WebManageMenu.SubWebManageMenus = WebManageMenusTree(WebManageMenu);
            return WebManageMenu;
        }

        List<WebManageMenus> WebManageMenusTree(WebManageMenus WebManageMenus)
        {
            return (from data1 in WebManageMenusCache
                    where data1.ParentID == WebManageMenus.MenuID
                    select new WebManageMenus_Ext
                    {
                        MenuID = data1.MenuID,
                        MenuName = data1.MenuName,
                        SortNumber = data1.SortNumber,
                        LinkUrl = data1.LinkUrl,
                        MenuLevel = data1.MenuLevel,
                        SubWebManageMenus = WebManageMenusTree(data1),
                        About = data1.About
                    }
            ).ToList<WebManageMenus>();
        }

        public PagedList<WebManageMenus> GetWebManageMenus(Expression<Func<WebManageMenus, bool>> predicate, PagingParam pp)
        {
            var linq = base.GetEntities<WebManageMenus>();
            if (predicate != null)
                linq = linq.Where(predicate);
            return linq.ToPagedList(pp.PageIndex, pp.PageSize);
        }
    }
}